package com.xm.dao.impl;

import com.xm.dao.BanksDao;
import com.xm.po.Banks;
import com.xm.util.BaseDao;
import com.xm.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2020-08-07-11:59
 * @description 银行卡数据操作接口实现类
 */
public class BanksDaoImpl implements BanksDao {
    @Override
    public List<Banks> getAll() {
        Connection conn = DButil.getConnection();
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Banks>list=new ArrayList<>();
        String sql="select * from banks where bankFlag = 1;";
        try {
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            list= (List<Banks>) DButil.getRsData(Banks.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,pstmt,conn);
        }
        return list;
    }

    @Override
    public boolean add(Banks banks) {

        return DButil.insert(banks);
    }

    @Override
    public boolean update(Banks banks) {
        boolean result = DButil.update(banks);
        return result;
    }

    @Override
    public Banks findById(Integer id) {
        return (Banks) DButil.queryOne(Banks.class,id);
    }

    @Override
    public boolean deleteById(Integer id) {
        Connection conn = DButil.getConnection();
        PreparedStatement pstmt=null;
        String sql="delete from Banks where bankId=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setObject(1,id);
            return pstmt.executeUpdate()>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(null,pstmt,conn);
        }
        return false;
    }
}
